package com.example.shoppingcenter.Controller;

import com.example.shoppingcenter.POJO.Result;
import com.example.shoppingcenter.POJO.User;
import com.example.shoppingcenter.Service.cdService;
import com.example.shoppingcenter.Service.userService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/image")
public class cdController {
    @Resource
    private cdService cdService;
    @Resource
    private userService userService;
    @RequestMapping("/collect")
    @ResponseBody
    public ResponseEntity<Result> imageCollect(@RequestParam("imageBlob") MultipartFile imageBlob , HttpServletRequest request) throws IOException {
        // 获取前端传来的 Blob 数据
        int userId = userService.getUserByAccount((String)request.getSession().getAttribute("user")).getId();
        Result result = cdService.saveImage(imageBlob,userId);
        return ResponseEntity.ok(result);
    }

    @RequestMapping("/deleteImage")
    @ResponseBody
    public ResponseEntity<Result> deleteImage(Integer imageId) {
        return ResponseEntity.ok(cdService.deleteImage(imageId));
    }
}
